package com.ruoyi.project.mall.api;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.project.mall.service.MallNewService;
import com.ruoyi.project.mall.service.MallNewsClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/api/mallNews")
public class ApiMallNewController {

    @Autowired
    private MallNewService mallNewService;
    @Autowired
    private MallNewsClassService mallNewsClassService;

    /***
     * 咨询详情
     * @return
     */
    @PostMapping( "/getNewsInfo")
    @ResponseBody
    public R getNewsInfo(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("newsId") || StringUtils.isBlank(params.get("newsId").toString())


            ) {
                return R.error("参数错误");
            }

            return  mallNewService.getNewsInfo(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     * 根据分类id获取咨询列表  传空查全部
     * @param params
     * @return
     */
    @PostMapping( "/getNewsList")
    @ResponseBody
    public R getNewsList(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("classId")


            ) {
                return R.error("参数错误");
            }

            return  mallNewService.getNewsList(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     * 咨询分类
     * @return
     */
    @PostMapping( "/getNewsClass")
    @ResponseBody
    public R getNewsClass(){

        try{


            return  mallNewsClassService.getNewsClass();
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }
    /**
     * 热门咨询
     * @return
     */
    @PostMapping( "/getHotNews")
    @ResponseBody
    public R getHotNews(){

        try{


            return  mallNewService.getHotNews();
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }
}
